﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Dictionary.DataLayer
{
    public class ModifyWord
    {
        //Ham them tu vung moi
        public static bool Add(string fn,string english,string phonetic,string vietnamese)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath + "\\" + fn);
                XmlNode node = source.SelectSingleNode("/Dictionary/Word[English=\"" + english + "\"]");
                if (node != null)
                {
                    return false;
                }
                XmlNodeList root = source.GetElementsByTagName("Dictionary");
                XmlElement element = source.CreateElement("Word");
                XmlElement e = source.CreateElement("English");
                e.InnerText = english;
                element.AppendChild(e);
                e = source.CreateElement("Phonetic");
                e.InnerText = phonetic;
                element.AppendChild(e);
                e = source.CreateElement("Vietnamese");
                e.InnerText = vietnamese;
                element.AppendChild(e);
                root[0].AppendChild(element);
                source.Save(DataLayer.FileDir.dirPath + "\\" + fn);
                return true;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return false;
            }
        }

        //Ham sua tu vung
        public static bool Modify(string fn, string english, string phonetic, string vietnamese,string word)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath + "\\" + fn);
                XmlNode node = source.SelectSingleNode("/Dictionary/Word[English=\"" + english + "\"]");
                if ((node != null) && (string.Compare(english, word) != 0))
                {
                    return false;
                }
                node = source.SelectSingleNode("/Dictionary/Word[English=\"" + word + "\"]");
                if (node == null)
                {
                    return false;
                }
                foreach (XmlElement e in node)
                {
                    if (e.LocalName == "English")
                        e.InnerText = english;
                    else
                        if (e.LocalName == "Phonetic")
                            e.InnerText = phonetic;
                        else
                            e.InnerText = vietnamese;
                }
                source.Save(DataLayer.FileDir.dirPath + "\\" + fn);
                return true;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return false;
            }
        }

        //Ham xoa tu vung
        public static bool Delete(string fn, string word)
        {
            try
            {
                XmlDocument source = new XmlDocument();
                source.Load(DataLayer.FileDir.dirPath + "\\" + fn);
                XmlNode node = source.SelectSingleNode("/Dictionary/Word[English=\"" + word + "\"]");
                if (node == null)
                {
                    return false;
                }
                else
                {
                    source.DocumentElement.RemoveChild(node);
                    source.Save(DataLayer.FileDir.dirPath + "\\" + fn);
                    return true;
                }
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                return false;
            }
        }
    }
}
